home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / mxlibs / dwstk102 / playdwd.bas < prev    next >
BASIC Source File  |  1995-04-12  |  5KB  |  190 lines

  1. '******************************************************************************
  2. 'File:      playdwd.bas
  3. 'Version:   1.02
  4. 'Tab stops: every 2 columns
  5. 'Project:   DWD Player
  6. 'Copyright: 1994-1995 DiamondWare, Ltd.  All rights reserved.
  7. 'Written:   Erik Lorenzen & Don Lemmons
  8. 'Purpose:   Contains simple example code to show how to load/play a .DWD file
  9. 'History:   94/10/21 KW Started playdwd.c
  10. '           94/11/12 DL Translated to BASIC
  11. '           95/01/12 EL Cleaned up & Finalized
  12. '           95/03/22 EL Finalized for 1.01
  13. '           95/04/11 EL Finalized for 1.02
  14. '
  15. 'Notes
  16. '-----
  17. 'This code isn't really robust when it comes to standard error checking
  18. 'and particularly recovery, software engineering technique, etc.  A buffer
  19. 'is statically allocated.  A better technique would be to use fstat() or stat()
  20. 'to determine the file's size then malloc(size).    The STK will handle songs
  21. 'larger than 64K (but not digitized sounds).  Obviously, you'd need to fread()
  22. 'such a file in chunks, or write some sort of hfread() (huge fread).  Also,
  23. 'exitting and cleanup is not handled robustly in this code.  The code below can
  24. 'only be validated by extremely careful scrutiny to make sure each case is
  25. 'handled properly.  A better method would the use of C's atexit function.
  26. '
  27. 'But all such code would make this example file less clear; its purpose was
  28. 'to illustrate how to call the STK, not how to write QA-proof software.
  29. '******************************************************************************/
  30.  
  31.  
  32.  
  33. '$INCLUDE: 'dws.bi'
  34. '$INCLUDE: 'err.bi'
  35.  
  36.  
  37.  
  38. TYPE BUFFTYP
  39.     buf AS STRING * 32767
  40. END TYPE
  41.  
  42.  
  43.  
  44. 'DECLARE VARIABLES
  45.     COMMON SHARED dov     AS dwsDETECTOVERRIDES
  46.     COMMON SHARED dres    AS dwsDETECTRESULTS
  47.     COMMON SHARED ideal AS dwsIDEAL
  48.     COMMON SHARED dplay AS dwsDPLAY
  49.  
  50.  
  51.  
  52. DIM SHARED buffer(0) AS BUFFTYP 'set aside string area for song to load into
  53.                                                                 'by doing it this way we give QBasic the
  54.                                                                 'opportunity to place the song into far mem
  55. 'START OF MAIN
  56.  
  57.     PRINT
  58.     PRINT "PLAYDWD 1.02 is Copyright 1994-95, DiamondWare, Ltd."
  59.     PRINT "All rights reserved."
  60.     PRINT : PRINT : PRINT
  61.  
  62.     filename$ = LTRIM$(RTRIM$(COMMAND$))
  63.     IF filename$ = "" THEN
  64.         PRINT "Usage PLAYDWD <dwd-file>"
  65.         GOTO ProgramExit
  66.     END IF
  67.  
  68.     OPEN filename$ FOR BINARY AS #1 LEN = 1
  69.     filelen = LOF(1)
  70.     CLOSE #1
  71.  
  72.     IF filelen = 0 THEN
  73.         PRINT "File Not Found"
  74.         GOTO ProgramExit
  75.     END IF
  76.  
  77.     IF filelen > 32767 THEN
  78.         PRINT "File Too Big"
  79.         GOTO ProgramExit
  80.     END IF
  81.  
  82.     OPEN filename$ FOR BINARY AS #1 LEN = 1
  83.     GET #1, 1, buffer(0).buf
  84.     CLOSE #1
  85.  
  86.     'We need to set every field to -1 in dwsDETECTOVERRIDES struct; this
  87.     'tells the STK to autodetect everything.  Any other value
  88.     'overrides the autodetect routine, and will be accepted on
  89.     'faith, though the STK will verify it if possible.
  90.  
  91.     dov.baseport = -1
  92.     dov.digdma     = -1
  93.     dov.digirq     = -1
  94.  
  95.     IF DWSDetectHardWare(dov, dres) = 0 THEN
  96.         errDisplay
  97.         GOTO ProgramExit
  98.     END IF
  99.  
  100.  
  101.     IF (dres.capability AND dwscapabilityDIG) <> dwscapabilityDIG THEN
  102.  
  103.         IF ((dres.baseport <> 904) AND (dres.baseport <> -1)) THEN
  104.             PRINT dres.baseport
  105.             PRINT "The sound hardware supports digitized sound playback,"
  106.             PRINT "but we couldn't find the DMA channel and/or IRQ level."
  107.         ELSE
  108.             PRINT "Support for digitized playback not found."
  109.         END IF
  110.  
  111.         GOTO ProgramExit
  112.  
  113.     END IF
  114.  
  115.  
  116.     'The "ideal" struct tells the STK how you'd like it to initialize the
  117.     'sound hardware.  In all cases, if the hardware won't support your     r
  118.     'request, the STK will go as close as possible.  For example, not all
  119.     'sound boards will support al sampling rates (some only support 5 or
  120.     '6 discrete rates).
  121.  
  122.     ideal.musictyp     = 0         '0=No music, 1=OPL2
  123.     ideal.digtyp         = 8         '0=No Dig, 8=8bit
  124.     ideal.digrate      = 5000  'sampling rate, in Hz
  125.                                                      'we could have called dws_DGetRateFromDWD
  126.                                                      'before initing the STK to get the correct rate
  127.     ideal.dignvoices = 16      'number of voices (up to 16)
  128.     ideal.dignchan     = 1         '1=mono
  129.  
  130.     IF dwsInit(dres, ideal) = 0 THEN
  131.         errDisplay
  132.         GOTO ProgramKill
  133.     END IF
  134.  
  135.     'Set master vol to about 4/5ths of max
  136.     IF dwsXMaster(200) = 0 THEN
  137.         errDisplay
  138.     END IF
  139.  
  140.     soundseg% = VARSEG(buffer(0).buf)
  141.     soundoff% = VARPTR(buffer(0).buf)
  142.     pointer&    = soundseg% * 256 ^ 2 + soundoff%  'make pointer
  143.  
  144.     dplay.snd          = pointer&
  145.     dplay.count      = 1                '0=infinite loop, 1-N num times to play sound
  146.     dplay.priority = 1000
  147.     dplay.presnd     = 0
  148.  
  149.     IF dwsDGetRateFromDWD(pointer&, ideal.digrate) = 0 THEN
  150.         errDisplay
  151.         GOTO ProgramKill
  152.     END IF
  153.  
  154.     IF dwsDSetRate(ideal.digrate)  = 0 THEN
  155.         errDisplay
  156.         GOTO ProgramKill
  157.     END IF
  158.  
  159.     IF dwsDPlay(dplay)    = 0 THEN
  160.         errDisplay
  161.         GOTO ProgramKill
  162.     END IF
  163.  
  164.     result% = dwsDSOUNDSTATUSPLAYING
  165.     DO UNTIL (result%  AND dwsDSOUNDSTATUSPLAYING) <> dwsDSOUNDSTATUSPLAYING
  166.         IF dwsDSoundStatus(dplay.soundnum, result%) = 0 THEN
  167.             errDisplay
  168.             GOTO ProgramKill
  169.         END IF
  170.     LOOP
  171.  
  172.     ProgramKill:
  173.  
  174.     IF dwsKill = 0 THEN
  175.         errnum = dwsErrNo
  176.         errDisplay
  177.  
  178.         'If an error occurs here, it's either dws_Kill_CANTUNHOOKISR
  179.         'or dws_NOTINITTED.  If it's dws_Kill_CANTUNHOOKISR the user
  180.         'must remove his tsr, and dws_Kill must be called again.  If it's
  181.         'dws_NOTINITTED, there's nothing to worry about at this point.
  182.         IF errnum = dwsKillCANTUNHOOKISR THEN
  183.             GOTO ProgramKill
  184.         END IF
  185.     END IF
  186.  
  187.     ProgramExit:
  188.  
  189. END
  190.